[网络流24题]飞行员配对方案问题

2020-01-29
网络流24题

题意

求二分图最大匹配

题解

复习一下模板

调试记录

bfs中vis的位置:要记录的是右边的有没有在这次匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdio>
#include <cstring>
const int maxn = 10005;
using namespace std;
struct E{
int to, nxt;
}e[maxn];
int head[maxn], tot = 0;
void addedge(int u, int v){
e[++tot].to = v, e[tot].nxt = head[u];
head[u] = tot;
}
int m, n, u, v, ans;
bool vis[maxn]; int mat[maxn];
bool dfs(int cur){
for (int i = head[cur]; i; i = e[i].nxt){
int v = e[i].to;
if (vis[v]) continue;
vis[v] = 1;
if (dfs(mat[v]) || !mat[v]){
mat[v] = cur;
mat[cur] = v;
return 1;
}
}
return 0;
}
int main(){
scanf("%d%d", &m, &n);
scanf("%d%d", &u, &v);
while (u != -1){
addedge(u, v);
scanf("%d%d", &u, &v);
}
for (int i = 1; i <= m; i++){
memset(vis, 0, sizeof vis);
ans += dfs(i);
}
if (!ans) puts("No Solution!");
else{
printf("%d\n", ans);
for (int i = 1; i <= n; i++)
if (mat[i] > i) printf("%d %d\n", i, mat[i]);
}
return 0;
}